//
//  LGCustomCellData.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/23.
//

#import "LGCustomCellData.h"

@interface LGCustomCellData()

/**   */
@property (nonatomic , assign)CGFloat contentHeight;

@end

@implementation LGCustomCellData
+ (TUIMessageCellData *)getCellData:(V2TIMMessage *)message{
    LGCustomCellData *cellData = [[LGCustomCellData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming];
    cellData.showName = NO;
    cellData.showAvatar = YES;
    cellData.innerMessage = message;
    cellData.msgID = message.msgID;
    cellData.avatarUrl = [NSURL URLWithString:message.faceURL];
    cellData.customDic = [ObjectTool jsonToObject:message.customElem.extension];
    return cellData;
}

/**
 * Getting the display string according to the message
 */
+(NSString *)getDisplayString:(V2TIMMessage *)message{
    NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
    if (!param || ![param isKindOfClass:[NSDictionary class]]) {
        return @"[自定义消息]";
    }
    NSString *businessID = param[BussinessID];
    if([businessID isEqualToString:@"TIMCustom_01"]){
        return @"[患者档案]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_02"]){
        return @"[咨询提示]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_03"]){
        return @"[咨询开始]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_05"]){
        return @"[延长时长]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_07"]){
        return @"[咨询结束]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_08"]){
        return @"[推荐医生]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_09"]){
        return @"[推荐服务]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_10"]){
        return @"[发送评估]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_11"]){
        return @"[咨询时间]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_12"]){
        return @"[提示咨询]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_13"]){
        return @"[咨询完成]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_15"]){
        return @"[咨询提示]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_16"]){
        return @"[未接通]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_17"]){
        return @"[随访建议]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_18"]){
        return @"[咨询取消]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_20"]){
        return @"[开具医嘱]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_30"]){
        return @"[在线候诊室已开启]";
        
    }else if ([businessID isEqualToString:@"TIMCustom_31"]){
        return @"[医生已开始按序咨询]";
        
    }
    
    return @"[自定义消息]";
}

-(instancetype)initWithDirection:(TMsgDirection)direction{
    self = [super initWithDirection:direction];
    if (self) {
//        self.showAvatar = YES;
        
    }
    return self;
}

-(CGFloat)caculateContentHeight{
    CGFloat height = 60;
    if ([self.reuseId isEqualToString:@"TIMCustom_01"]){
        //发起咨询提示
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_02"]){
        //患者卡片
        self.showAvatar = YES;
        NSString *content = LGNSString(self.customDic[@"msgcontent_doctor"][@"content"]);
        CGFloat contentH = [content textHeightWithFont:LGFont(12) maxWidth:Screen_W-viewPix(126)];
        height = contentH+viewPix(131)+viewPix(16);
       
    }else if ([self.reuseId isEqualToString:@"TIMCustom_03"]){
        //咨询已开启
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        height = viewPix(90);
        
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_05"]){
        //延长咨询时间
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
       
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_07"]){
        //咨询已结束
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        height = viewPix(90);
        
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_08"]){
        //推荐医生
        NSLog(@">>???>>>%@",self.customDic);
        height = viewPix(130);
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_09"]){
        //推荐服务
        height = viewPix(130);
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_10"]){
        //发送评估
        NSArray *evaArray = self.customDic[@"msgcontent_doctor"][@"scales"];
        height = viewPix(75)+viewPix(30)*evaArray.count;
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_11"]){
        //视频咨询-发起就诊
        height = viewPix(130);
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_12"]){
        //视频咨询-发起就诊提示
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_13"]){
        //问诊结束
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        height = viewPix(90);
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_15"]){
        //视频通话描述
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        height = viewPix(90);
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_16"]){
        //视频通话描述
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_17"]){
        //随访建议
        NSString *medicContent = LGNSString(self.customDic[@"msgcontent_doctor"][@"drug_plan"]);
        CGFloat medicHeight = [medicContent textHeightWithFont:LGFont(12) maxWidth:Screen_W-viewPix(124)];
        NSString *followContent = LGNSString(self.customDic[@"msgcontent_doctor"][@"followup_content"]);
        CGFloat followHeight = [followContent textHeightWithFont:LGFont(12) maxWidth:Screen_W-viewPix(124)];
        height = medicHeight+followHeight+viewPix(100)+40;
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_18"]){
        //咨询取消
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_20"]){
        //发送医嘱
        NSString *content = LGNSString(self.customDic[@"msgcontent_doctor"][@"content"]);
        CGFloat contentH = [content textHeightWithFont:LGFont(14) maxWidth:Screen_W-viewPix(124)];
        height = viewPix(75)+contentH;
    }else if ([self.reuseId isEqualToString:@"TIMCustom_30"]){
        //在线候诊室描述
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        height = viewPix(90);
    }else if ([self.reuseId isEqualToString:@"TIMCustom_31"]){
        //在线候诊室描述
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        height = viewPix(90);
        
    }else if ([self.reuseId isEqualToString:@"TIMCustom_32"]){
        //发送量表
        self.showAvatar = NO;
        self.cellLayout =  [TUIMessageCellLayout systemMessageLayout];
        height = viewPix(90);
    }
    return height;
}

+(CGFloat)caculateCellHeight:(NSDictionary *)customDic{
    NSString *reuseId = LGNSString(customDic[@"msgtype"]);
    CGFloat height = 60;
     if ([reuseId isEqualToString:@"TIMCustom_02"]){
        //患者卡片
     
        NSString *content = LGNSString(customDic[@"msgcontent_doctor"][@"content"]);
        CGFloat contentH = [content textHeightWithFont:LGFont(12) maxWidth:Screen_W-viewPix(126)];
        height = contentH+viewPix(131)+viewPix(16);
       
    }else if ([reuseId isEqualToString:@"TIMCustom_03"]){
        
        height = viewPix(90);
        
        
    }else if ([reuseId isEqualToString:@"TIMCustom_07"]){
        //咨询已结束
        height = viewPix(90);
        
    }else if ([reuseId isEqualToString:@"TIMCustom_08"]){
        //推荐医生
        height = viewPix(130);
        
    }else if ([reuseId isEqualToString:@"TIMCustom_09"]){
        //推荐服务
        height = viewPix(130);
        
    }else if ([reuseId isEqualToString:@"TIMCustom_10"]){
        //发送评估
        NSArray *evaArray = customDic[@"msgcontent_doctor"][@"scales"];
        height = viewPix(75)+viewPix(30)*evaArray.count;
        
    }else if ([reuseId isEqualToString:@"TIMCustom_11"]){
        //视频咨询-发起就诊
        height = viewPix(130);
        
    }else if ([reuseId isEqualToString:@"TIMCustom_13"]){
        //问诊结束

        height = viewPix(90);
        
    }else if ([reuseId isEqualToString:@"TIMCustom_15"]){
        //视频通话描述

        height = viewPix(90);
        
    }else if ([reuseId isEqualToString:@"TIMCustom_17"]){
        //随访建议
        NSString *medicContent = LGNSString(customDic[@"msgcontent_doctor"][@"drug_plan"]);
        CGFloat medicHeight = [medicContent textHeightWithFont:LGFont(12) maxWidth:Screen_W-viewPix(124)];
        NSString *followContent = LGNSString(customDic[@"msgcontent_doctor"][@"followup_content"]);
        CGFloat followHeight = [followContent textHeightWithFont:LGFont(12) maxWidth:Screen_W-viewPix(124)];
        height = medicHeight+followHeight+viewPix(100)+40;
        
    }else if ([reuseId isEqualToString:@"TIMCustom_20"]){
        //发送医嘱
        NSString *content = LGNSString(customDic[@"msgcontent_doctor"][@"content"]);
        CGFloat contentH = [content textHeightWithFont:LGFont(14) maxWidth:Screen_W-viewPix(124)];
        height = viewPix(75)+contentH;
        
    }else if ([reuseId isEqualToString:@"TIMCustom_30"]){
        //在线候诊室描述
        height = viewPix(90);
    }else if ([reuseId isEqualToString:@"TIMCustom_31"]){
        //在线候诊室描述
        height = viewPix(90);
    }
    return height;
}



-(CGFloat)estimatedHeight{
//    NSLog(@">>>???>>%.2f",self.contentHeight);
    return [self caculateContentHeight];
}

-(CGSize)contentSize{
//    NSLog(@">>>???>>>%@--%f",self.reuseId,[self estimatedHeight]);
    return CGSizeMake(Screen_W-viewPix(80), [self estimatedHeight]-viewPix(16));
}

@end
